#!/bin/bash
MONGO_LOG=/tmp/mongo.log
mongo 127.0.0.1:27017/admin > ${MONGO_LOG}  <<EOF


use connected-pdf
db.createCollection('DRMPermission')
db.DRMPermission.dropIndexes()
db.DRMPermission.ensureIndex({"_etag":1},{"name":"IX_etag"})
db.DRMPermission.ensureIndex({"cDocID":1},{"name":"IX_cDocID"})
db.DRMPermission.ensureIndex({"userAttribute.userId":1},{"name":"IX_userId"})
db.DRMPermission.ensureIndex({"isRevoke":1},{"name":"IX_isRevoke"})

db.createCollection('DocumentExtra')
db.DocumentExtra.dropIndexes()
db.DocumentExtra.ensureIndex({"_etag":1},{"name":"IX_etag"})
db.DocumentExtra.ensureIndex({"cDocID":1},{"name":"IX_cDocID"})


db.createCollection('PermissionMessage')
db.PermissionMessage.dropIndexes()
db.PermissionMessage.ensureIndex({"_etag":1},{"name":"IX_etag"})
db.PermissionMessage.ensureIndex({"userId":1,"cDocID":1,"changeType":1,"isEffective":1,"isDelivery":1,"updateTime":-1})
db.PermissionMessage.ensureIndex({"typeContent.requesterId":1,"changeType":1,"isEffective":1,"isDelivery":1,"updateTime":-1})
db.PermissionMessage.ensureIndex({"changeType":1, "userId":1, "delTag":1})
db.PermissionMessage.ensureIndex({"changeType":1, "typeContent.requesterId":1})

db.createCollection('doc-logs')
db['doc-logs’].dropIndexes()
db['doc-logs'].ensureIndex({"_etag":1},{"name":"IX_etag"})
db['doc-logs'].ensureIndex({"cDocID":1},{"name":"IX_cDocID"})
db['doc-logs'].ensureIndex({"cUserID":1},{"name":"IX_cUserID"})
db['doc-logs'].ensureIndex({"cVersionID":1},{"name":"IX_cVersionID"})
db['doc-logs'].ensureIndex({"operatorType":1},{"name":"IX_operatorType"})
db['doc-logs'].ensureIndex({"cAppID":1},{"name":"IX_cAppID"})
db['doc-logs'].ensureIndex({"docName":1},{"name":"IX_docName"})


db.createCollection('doc-versions')
db['doc-versions’].dropIndexes()
db['doc-versions'].ensureIndex({"_etag":1},{"name":"IX_etag"})
db['doc-versions'].ensureIndex({"cDocID":1},{"name":"IX_cDocID"})
db['doc-versions'].ensureIndex({"cVersionID":1},{"name":"IX_cVersionID"})
db['doc-versions'].ensureIndex({"cUserID":1},{"name":"IX_cUserID"})
db['doc-versions'].ensureIndex({"parentID":1},{"name":"IX_parentID"})
db['doc-versions'].ensureIndex({"cDocID":1,"sVersionId":1},{"unique":true,"dropDups":true})


db.createCollection('docHomePage')
db.docHomePage.dropIndexes()
db.docHomePage.ensureIndex({"_etag":1},{"name":"IX_etag"})
db.docHomePage.ensureIndex({"cDocID":1},{"name":"IX_cDocID"})
db.docHomePage.ensureIndex({"cUserID":1},{"name":"IX_cUserID"})
db.docHomePage.ensureIndex({"cGroupID":1},{"name":"IX_cGroupID"})


db.createCollection('docSearchMessage')
db.docSearchMessage.dropIndexes()
db.docSearchMessage.ensureIndex({"_etag":1},{"name":"IX_etag"})
db.docSearchMessage.ensureIndex({"cUserID":1,"isEffective":1,"isDelivery":1,"updateTime":-1})
db.docSearchMessage.ensureIndex({"changeType":1, "cUserID":1})
db.docSearchMessage.ensureIndex({"changeType":1, "requestUserId":1})


db.createCollection('documents')
db.documents.dropIndexes()
db.documents.ensureIndex({"_etag":1},{"name":"IX_etag"})
db.documents.ensureIndex({"cUserID":1},{"name":"IX_cUserID"})
db.documents.ensureIndex({"cDocID":1},{"unique":true,"dropDups":true},{"name":"IX_cDocID"})
db.documents.ensureIndex({"parentID":1},{"name":"IX_parentID"})
db.documents.ensureIndex({"cAppID":1},{"name":"IX_cAppID"})


db.createCollection('messageTemplate')
db.messageTemplate.dropIndexes()
db.messageTemplate.ensureIndex({"_etag":1},{"name":"IX_etag"})

db.createCollection('userOnline')
db.userOnline.dropIndexes()
db.userOnline.ensureIndex({"_etag":1},{"name":"IX_etag"})
db.userOnline.ensureIndex({"cAppID":1},{"name":"IX_cAppID"})
db.userOnline.ensureIndex({"userId":1},{"name":"IX_userId"})


db.createCollection('PermissionMessageExtra')
db.PermissionMessageExtra.dropIndexes()
db.PermissionMessageExtra.ensureIndex({"_etag":1},{"name":"IX_etag"})
db.PermissionMessageExtra.ensureIndex({"userId":1,"cAppID":1,"isEffective":1,"isDelivery":1,"updateTime":-1})


db.createCollection('personnelRelationship')
db.personnelRelationship.dropIndexes()
db.personnelRelationship.ensureIndex({"_etag":1},{"name":"IX_etag"})


db.createCollection('homepageMessage')
db.homepageMessage.dropIndexes()
db.homepageMessage.ensureIndex({"_etag":1},{"name":"IX_etag"})
db.homepageMessage.ensureIndex({"cUserID":1,"isEffective":1,"isDelivery":1,"updateTime":-1})
db.homepageMessage.ensureIndex({"requesterID":1, "changeType":1})
db.homepageMessage.ensureIndex({"cUserID":1, "changeType":1})

db.createCollection('shareReviewBase')
db.shareReviewBase.dropIndexes()
db.shareReviewBase.ensureIndex({"_etag":1},{"name":"IX_etag"})
db.shareReviewBase.ensureIndex({"reviewSessionID":1},{"name":"IX_reviewSessionID"})
db.shareReviewBase.ensureIndex({"cDocID":1},{"name":"IX_cDocID"})


db.createCollection('shareReviewParticipants')
db.shareReviewParticipants.dropIndexes()
db.shareReviewParticipants.ensureIndex({"_etag":1},{"name":"IX_etag"})
db.shareReviewParticipants.ensureIndex({"reviewSessionID":1},{"name":"IX_reviewSessionID"})
db.shareReviewParticipants.ensureIndex({"cUserID":1},{"name":"IX_cUserID"})


db.createCollection('shareReviewAnnots')
db.shareReviewAnnots.dropIndexes()
db.shareReviewAnnots.ensureIndex({"reviewSessionID":1},{"name":"IX_reviewSessionID"})
db.shareReviewAnnots.ensureIndex({"_etag":1},{"name":"IX_etag"})
db.shareReviewAnnots.ensureIndex({"cUserID":1},{"name":"IX_cUserID"})
db.shareReviewAnnots.ensureIndex({"updateTime":1},{"name":"IX_updateTime"})

db.createCollection('subLogs')
db.subLogs.dropIndexes()
db.subLogs.ensureIndex({"_etag":1},{"name":"IX_etag"})
db.subLogs.ensureIndex({"cUserID":1,"cVersionID":1,"role":1},{"name":"cUserID_cVersionID_role"})


db.createCollection('connectedPDFClient')
db.connectedPDFClient.dropIndexes()
db.connectedPDFClient.ensureIndex({"_etag":1},{"name":"IX_etag"})
db.connectedPDFClient.ensureIndex({"app._id":1},{"name":"IX_cAppID"})
db.connectedPDFClient.ensureIndex({"app.productName":1},{"name":"IX_productName"})


db.createCollection('userExtend')
db.userExtend.dropIndexes()
db.userExtend.ensureIndex({"_etag":1},{"name":"IX_etag"})
db.userExtend.ensureIndex({"cUserID":1},{"name":"IX_cUserID"},{unique:true});



db.createCollection('openMessage')
db.openMessage.ensureIndex({"cDocID":1},{"name":"IX_cDocID"})
db.openMessage.ensureIndex({"_etag":1},{"name":"IX_etag"})

db.createCollection('optionPermissions')
db.optionPermissions.ensureIndex({"cDocID":1},{"name":"IX_cDocID"})
db.optionPermissions.ensureIndex({"cUserID":1},{"name":"IX_cUserID"})
db.optionPermissions.ensureIndex({"_etag":1},{"name":"IX_etag"})
EOF

echo "MongoDB initialized, log file is at ${MONGO_LOG}."
touch /tmp/MONGOINITED
